package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookClassifyMapper;
import com.kgc.novel.domain.entity.BookClassify;
import com.kgc.novel.domain.param.BookClassifyParam;
import com.kgc.novel.service.book.BookClassifyService;
import com.kgc.novel.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author mac
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BookClassifyServiceImpl extends ServiceImpl<BookClassifyMapper, BookClassify> implements BookClassifyService {

    @Resource
    private BookClassifyMapper bookClassifyMapper;

    @Override
    public Res selectAll(BookClassifyParam bookClassifyParam) {
        Objects.isNull(bookClassifyParam);
        Page<BookClassify> page = new Page<>(bookClassifyParam.getPageNo(), bookClassifyParam.getPageSize());
        LambdaQueryWrapper<BookClassify> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(bookClassifyParam.getClassifyName()),BookClassify::getClassifyName,bookClassifyParam.getClassifyName());
        Page<BookClassify> pageList = bookClassifyMapper.selectPage(page, lambdaQueryWrapper);
        return Res.returnData(pageList,pageList.getTotal());
    }

    @Override
    public Res selectAllNoPage() {
        QueryWrapper<BookClassify> queryWrapper = new QueryWrapper<>();
        List<BookClassify> list = bookClassifyMapper.selectList(queryWrapper);
        return Res.returnData(list);
    }

    @Override
    public Res getSingle(String id) {
        BookClassify bookClassify = bookClassifyMapper.selectById(id);
        if (bookClassify != null){
            return Res.returnData(bookClassify);
        }
        return Res.returnMsg("暂无该小说分类");
    }

    @Override
    public Res addSingle(BookClassify bookClassify) {
        LambdaQueryWrapper<BookClassify> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookClassify::getClassifyName,bookClassify.getClassifyName());

        BookClassify bookClassify1 = bookClassifyMapper.selectOne(lambdaQueryWrapper);
        if (bookClassify1 != null){
            return Res.returnMsg("该小说分类已存在，请重新新增！");
        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        bookClassify.setId(String.valueOf(id));
        bookClassifyMapper.insert(bookClassify);
        return Res.returnMsg("新增成功!");
    }

    @Override
    public Res updateSingle(BookClassify bookClassify) {
        LambdaQueryWrapper<BookClassify> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookClassify::getClassifyName,bookClassify.getClassifyName());

        BookClassify bookClassify1 = bookClassifyMapper.selectOne(lambdaQueryWrapper);
        if (bookClassify1 == null){
            bookClassifyMapper.updateById(bookClassify);
            return Res.returnMsg("修改成功！");
        }
        BookClassify bookClassify2 = bookClassifyMapper.selectById(bookClassify.getId());
        if (bookClassify.getId().equals(bookClassify2.getId()) && bookClassify2.getClassifyName().equals(bookClassify.getClassifyName())){
            bookClassifyMapper.updateById(bookClassify);
            return Res.returnMsg("修改成功！");
        }
        return Res.returnMsg("该小说已存在，请重新修改！");
    }

    @Override
    public Res deleteSingle(String id) {
        int i = bookClassifyMapper.deleteById(id);
        if (i != -1){
            return Res.returnMsg("删除成功！");
        }else {
            return Res.returnMsg("删除失败！");
        }
    }

    @Override
    public Res deleteAll(List<String> ids) {
        int i = bookClassifyMapper.deleteBatchIds(ids);
        if (i != -1){
            return Res.returnMsg("批量删除成功");
        }else {
            return Res.returnMsg("批量删除失败");
        }
    }
}
